//
//  TenTableViewController.m
//  天下事
//
//  Created by dlios on 15-7-11.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "TenTableViewController.h"
#import "AFNetworking.h"
#import "MJRefresh.h"
#import "UIImageView+WebCache.h"
#import "NewsCell.h"
#import "DataHandel.h"
#import "NetWorkTools.h"
#import "SecMyCell.h"
#import "ThirdMyCell.h"
#import "BigImageCell.h"
#import "BigImageCollectionViewcell.h"
#import "BigImageCellB.h"

@interface TenTableViewController()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic, retain)UICollectionView *collectview;
@property(nonatomic, retain)UIPageControl *pagecontroler;
@property(nonatomic, retain)NSMutableArray *temparr;

@end

@implementation TenTableViewController
- (void)dealloc
{
    [_urlString release];
    [_arrayList release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"SecMyCell" bundle:nil] forCellReuseIdentifier:@"SecMyCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ThirdMyCell" bundle:nil] forCellReuseIdentifier:@"ThirdMyCell"];
//    [self.tableView registerNib:[UINib nibWithNibName:@"BigImageCell" bundle:nil] forCellReuseIdentifier:@"BigImageCell"];
    [self.tableView registerClass:[BigImageCell class] forCellReuseIdentifier:@"BigImageCell"];
    [self.tableView registerClass:[BigImageCellB class] forCellReuseIdentifier:@"BigImageCellB"];
    [self loadData];
//    self.pagecontroler = [[UIPageControl alloc]init];
//    self.pagecontroler.currentPageIndicatorTintColor = [UIColor cyanColor];
//    self.pagecontroler.pageIndicatorTintColor = [UIColor blackColor];
//    self.pagecontroler.numberOfPages = 2;
//    //             self.pagecontroler.backgroundColor = [UIColor redColor];
//    [self.view addSubview:self.pagecontroler];
//    [self creatHeaderview];

}

- (void)creatHeaderview{
    self.automaticallyAdjustsScrollViewInsets = NO;
    UIView *headerview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 245)];
    headerview.backgroundColor = [UIColor yellowColor];
//    self.tableView.tableHeaderView = headerview;
    
    
//    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
//    layout.itemSize = CGSizeMake(self.view.frame.size.width, 245);
//  
//    self.collectview = [[UICollectionView alloc]initWithFrame:headerview.frame collectionViewLayout:layout];
//    self.collectview.pagingEnabled = YES;
//    
//    self.collectview.delegate = self;
//    self.collectview.dataSource = self;
//    self.collectview.showsHorizontalScrollIndicator = NO;
//    layout.minimumLineSpacing = 0;
//    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
//    self.collectview.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
//    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//    self.collectview.backgroundColor = [UIColor redColor];
//    [self.collectview registerClass:[BigImageCollectionViewcell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
}
- (void)pagechange{
    [self.collectview setContentOffset:CGPointMake(375 * self.pagecontroler.currentPage, 0) animated:YES];
    
}

- (void)setArrayList:(NSMutableArray *)arrayList{
    _arrayList = arrayList;
//    [self.tableView reloadData];
    [self.collectview reloadData];
}

- (BOOL)prefersStatusBarHidden{
    return YES;
}


- (void)loadData
{
    
    [[[NetWorkTools sharNetworkTools]GET:self.urlString parameters:nil success:^(NSURLSessionDataTask *task, NSDictionary* responseObject) {
                NSLog(@"%@",responseObject);
        //快速枚举遍历字典中所有key值
        NSString *key = [responseObject.keyEnumerator nextObject];
        
        NSArray *temArray = responseObject[key];
        NSMutableArray *arrayM = [[NSMutableArray alloc]init];
        self.arrayList = [[NSMutableArray alloc]init];
        //快速枚举
        [temArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            
            NewsModel *news = [NewsModel newsModelwithDic:obj];
            [arrayM addObject:news];
            
        }];
        self.arrayList = arrayM;
        [self.tableView reloadData];
    
        self.arr = [NSMutableArray array];
        for (int i = 0; i < self.arrayList.count; i++) {
            if ([self.arrayList[i] hasHead ]) {
                NSMutableArray *temarr = [[self.arrayList[i] imgextra]mutableCopy];
                for (NSDictionary *dic in temarr) {
                    [self.arr addObject:dic];
                }
            }
        }
    

        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"%@",error);
    }] resume];
    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 20;
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SecMyCell *cell2 = [tableView dequeueReusableCellWithIdentifier:@"SecMyCell"];
    NewsModel *nm = self.arrayList[indexPath.row];
    
    ThirdMyCell *cell3= [tableView dequeueReusableCellWithIdentifier:@"ThirdMyCell"];
    
    BigImageCell *cell1 = [tableView dequeueReusableCellWithIdentifier:@"BigImageCell"];
    
    BigImageCellB *cell4 = [tableView dequeueReusableCellWithIdentifier:@"BigImageCellB"];
    
    cell1.arrayList1 = self.arr;
    cell1.arrayList2 = self.arrayList;
    cell2.newsmodel = nm;
    cell3.nm = nm;
    cell4.nm = nm;
    
    if (nm.hasHead && nm.photosetID) {
        return cell1;
    }
    else if(nm.hasHead){
        return cell4;
    }
    else if (nm.imgextra) {
        //       NSLog(@"%@", nm.photosetID);
        NSLog(@"cell3");
        return cell3;
    }else{
        //        NSLog(@"cell2");
        return cell2;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NewsModel *newmodel = self.arrayList[indexPath.row];
    //return [NewsCell heightForRow:newmodel];
    if (newmodel.hasHead && newmodel.photosetID) {
        return 240;
    }
     if(newmodel.hasHead){
        return 200;
    }
     if (newmodel.imgextra) {
        return 130;
    }else
        return 100;
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UIViewController *vc = [[UIViewController alloc]init];
    vc.view.backgroundColor = [UIColor blueColor];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return self.arr.count;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    BigImageCollectionViewcell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewCell" forIndexPath:indexPath];
    NewsModel *nm = self.arrayList[indexPath.item];
    [cell.bigImg sd_setImageWithURL:[NSURL URLWithString:[self.arr[indexPath.item] objectForKey:@"imgsrc"]]];
    cell.BigImagetitle.text = nm.title;
    self.pagecontroler.numberOfPages = self.arr.count;
    return cell;


}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    NSInteger x = self.collectview.contentOffset.x / self.view.frame.size.width;
    self.pagecontroler.currentPage = x;
    
}

@end
